package vmcs.controller;

// Copyright 1997 ISS.
// The contents contained in this document may not be reproduced in any
// form or by any means, without the written permission of ISS, other
// than for the purpose for which it has been supplied.
//
// Author: Olivo Miotto, Pang Pingli
//
import java.awt.*;
import vmcs.utility.AccessManager;
import vmcs.entity.CashStoreItem;
import vmcs.entity.DrinksBrand;
import vmcs.entity.DrinksStoreItem;
import vmcs.boundary.MaintenancePanel;
import vmcs.boundary.MessageDialog;
import vmcs.utility.MoneyFormatter;
import vmcs.boundary.SimulatorControlPanel;
import vmcs.controller.statePattern.TransactionController;
import vmcs.utility.VMCSException;

public class MaintenanceController {

    private MainController mCtrl;
    private MaintenancePanel mpanel;
    private AccessManager am;

    public MaintenanceController(MainController mctrl) {
        mCtrl = mctrl;
        am = new AccessManager(this);
    }

    public MainController getMainController() {
        return mCtrl;
    }

    public MaintenancePanel getMaintenancePanel() {
        return mpanel;
    }

    /**
     * setup the maintenance panel and display it.
     */
    public void displayMaintenancePanel() {
        SimulatorControlPanel scp = mCtrl.getSimulatorControlPanel();
        if (mpanel == null) {
            mpanel = new MaintenancePanel((Frame) scp, this);
        }
        mpanel.display();
        mpanel.setActive(MaintenancePanel.DIALOG, true);  // setActive of password, invalid and valid display.
    }

    
    public AccessManager getAccessManager() {
        return am;
    }

    public void loginMaintainer(boolean st) {
        mpanel.displayPasswordState(st);
        mpanel.clearPassword();
        if (st == true) {
            // login successful
            mpanel.setActive(MaintenancePanel.WORKING, true);
            mpanel.setActive(MaintenancePanel.PSWD, false);
            StoreController sctrl = mCtrl.getStoreController();
            sctrl.setDoorState(false);
            //sctrl.displayMachineryPanel();
            SimulatorControlPanel scp = mCtrl.getSimulatorControlPanel();
            scp.setActive(SimulatorControlPanel.ACT_CUSTOMER, false);
            
            //set OnMaintenanceTransaction to trigger the statecntrl logic. 
            TransactionController.instance().OnMaintenanceTransaction(true);
        }
    }

    // invoked in CoinDisplayListener
    public void displayCoin(int idx) {
        StoreController sctrl = mCtrl.getStoreController();
        CashStoreItem item;
        try {
            item = (CashStoreItem) sctrl.getStoreItem(StoreController.CASH, idx);
            mpanel.getCoinDisplay().displayQty(idx, item.getQuantity());
        } catch (VMCSException e) {
            System.out.println("MaintenanceController.displayCoin:" + e);
        }

    }

    // invoked in DrinkDisplayListener;
    public void displayDrinks(int idx) {
        StoreController sctrl = mCtrl.getStoreController();
        DrinksStoreItem item;
        try {
            item = (DrinksStoreItem) sctrl.getStoreItem(StoreController.DRINK, idx);
            // mapnel.getDrinkDisplay().displayQuantity(idx, item.getQuantity());
            DrinksBrand db = (DrinksBrand) item.getContent();
            mpanel.getDrinksDisplay().displayQty(idx, item.getQuantity());
            mpanel.displayPrice(db.getPrice());
        } catch (VMCSException e) {
            System.out.println("MaintenanceController.displayDrink:" + e);
        }
    }

    // invoked by PriceDisplayListener
    public void setPrice(int pr) {
        StoreController sctrl = mCtrl.getStoreController();
        //  TransactionController  tctrl = mCtrl.getTransactionController();
        int curIdx;
        curIdx = mpanel.getCurIdx();
        try {
            sctrl.setPrice(curIdx, pr);
            mpanel.getDrinksDisplay().getPriceDisplay().setValue(MoneyFormatter.toMoney(pr));
            // tctrl.setPrice(curIdx, pr);
        } catch (VMCSException e) {
            System.out.println("MaintenanceController.setPrice:" + e);
        }
    }

    // TotalCashButtonListener
    public void getTotalCash() {
        StoreController sctrl = mCtrl.getStoreController();

        int tc;

        try {
            tc = sctrl.getTotalCash();
            mpanel.displayTotalCash(tc);

        } catch (VMCSException e) {
            System.out.println("MaintenanceController.getTotalCash:" + e);
        }
    }

    // TransferCashButtonListener
    // get all the cash from store and set store cash 0;
    public void transferAll() {
        StoreController sctrl = mCtrl.getStoreController();

        int cc;   // coin quantity;
        try {

            cc = sctrl.transferAll();
            mpanel.displayCoins(cc);
            sctrl.displayCoinStock();
            //sctrl.updateStoreDisplay(Store.CASH);
            // the cash qty current is displayed in the Maintenance panel needs to be update to be 0;
            // not required.
            mpanel.updateCurrentQtyDisplay(StoreController.CASH, 0);
        } catch (VMCSException e) {
            System.out.println("MaintenanceController.transferAll:" + e);
        }
    }

    // StoreViewerListener, not required;
    public void changeStoreQty(char type, int idx, int qty) {
        StoreController sctrl = mCtrl.getStoreController();

        try {
            mpanel.updateQtyDisplay(type, idx, qty);
            mpanel.initCollectCash();
            mpanel.initTotalCash();
        } catch (VMCSException e) {
            System.out.println("MaintenanceController.changeStoreQty:" + e);
        }
    }

    // exit button listener;
    public void logoutMaintainer() {

        StoreController sctrl = mCtrl.getStoreController();

        boolean ds = sctrl.isDoorClosed();

        if (ds == false) {
            MessageDialog msg = new MessageDialog(mpanel, MessageDialog.LOCK_DOOR);
            return;
        }

        mpanel.setActive(MaintenancePanel.DIALOG, true);
        SimulatorControlPanel smctrl = mCtrl.getSimulatorControlPanel();
        smctrl.setActive(SimulatorControlPanel.ACT_MAINTAINER, true);
        smctrl.setActive(SimulatorControlPanel.ACT_CUSTOMER, true);
        smctrl.setActive(SimulatorControlPanel.ACT_MACHINERY, true);
        TransactionController.instance().OnMaintenanceTransaction(false);
        mCtrl.getCustomerController().refresh();
    }

    public boolean closeDown() {
        StoreController sctrl = mCtrl.getStoreController();
        if (mpanel == null) {
            return true;
        }
        if (sctrl.isDoorClosed() == false) {
            MessageDialog msg = new MessageDialog(mpanel, MessageDialog.LOCK_DOOR);
            return false;
        }
        mpanel.closeDown();
        SimulatorControlPanel smctrl = mCtrl.getSimulatorControlPanel();
        smctrl.setActive(SimulatorControlPanel.ACT_MAINTAINER, true);
        smctrl.setActive(SimulatorControlPanel.ACT_CUSTOMER, true);
        smctrl.setActive(SimulatorControlPanel.ACT_MACHINERY, true);        
        TransactionController.instance().OnMaintenanceTransaction(false);        
        return true;
    }
}
